//
//  HUD.swift
//  Unibet
//
//  Created by HaoSheng on 2020/4/14.
//  Copyright © 2020 蜗牛. All rights reserved. 
//

import UIKit
import MBProgressHUD

public enum HudType{
    case none
    case goods
    case error
}

// @unchecked Sendable 我知道这个类不符合自动 Sendable 检查，但我保证它线程安全
// 多线程并发访问数据时，不会出现未同步的读写冲突
@available(iOS 13.0, *)
open class HUD: @unchecked Sendable {
    
    private static let shared = HUD()
    
    private init() { }
    
    @MainActor static var baseOffsetY = UIScreen.main.bounds.height/4
    
    /// 正在显示的HUD缓存
    @MainActor static var lodingHud: MBProgressHUD = MBProgressHUD()
    
    /// 菊花加载提示 view为nil显示在wind上
    public static func showHudInView(view: UIView? = nil, hint: String? = nil) {
        
        DispatchQueue.main.async { // 切换到主队列
            
            let hud = HUD.lodingHud;
            hud.label.text = hint ?? "";
            hud.label.font = UIFont.systemFont(ofSize: 16)
            
            if view == nil{
                getWindow.addSubview(hud)
            }else{
                view!.addSubview(hud);
            }
            
            hud.show(animated: true);
            hud.removeFromSuperViewOnHide = true;
            
        }
        
    }
    
    ///隐藏菊花加载提示
    public static func hideHud() {
        DispatchQueue.main.async { // 切换到主队列
            HUD.lodingHud.hide(animated: true);
        }
    }
    
    
    /// new一个菊花HUD返回
    @MainActor public static func showNewHudInView(view: UIView? = nil, hint: String? = nil) -> MBProgressHUD {
        
        let hud = MBProgressHUD();
        hud.label.text = hint ?? "";
        hud.label.font = UIFont.systemFont(ofSize: 16)
        
        if view == nil{
            getWindow.addSubview(hud)
        }else{
            view!.addSubview(hud);
        }
        
        hud.show(animated: true);
        hud.removeFromSuperViewOnHide = true;
        
        return hud
    }
    
    ///显示提示信息
    /// userEnabled = true 用户点击会被遮挡
    /// type 提示类型
    public static func showHint(hint: String?,userEnabled:Bool = false,type: HudType = .none) {
        HUD.shared.showHint(hint: hint, userEnabled: userEnabled, type: type)
    }
    
    ///显示提示信息
    /// userEnabled = true 用户点击会被遮挡
    public func showHint(hint: String?,userEnabled:Bool = false,type: HudType = .none) {
        guard let hint = hint, !hint.isEmpty else { return }
        
        switch type {
        case .none:
            showNoneHint(hint: hint, userEnabled: userEnabled)
        case .goods:
            showGoodHint(hint: hint, userEnabled: userEnabled)
        case .error:
            showErrorHint(hint: hint, userEnabled: userEnabled)
        }
    }
    
    
    ///显示提示信息
    /// userEnabled = true 用户点击会被遮挡
    func showNoneHint(hint: String?,userEnabled: Bool = false){
        
        DispatchQueue.main.async { [weak self] in  // 切换到主队列
            guard let self = self else { return }
            
            let view = getWindow
            
            let hud = MBProgressHUD.showAdded(to: view, animated: true);
            
            hud.isUserInteractionEnabled = userEnabled
            hud.mode = .text;
            hud.detailsLabel.text = hint;
            hud.detailsLabel.font = UIFont.systemFont(ofSize: 13)
//            hud.margin = 10;
            hud.removeFromSuperViewOnHide = true;
            // 显示到屏幕底部起1/4的位置
            hud.offset = CGPoint(x: 0, y: HUD.baseOffsetY)
            hud.hide(animated: true, afterDelay: 2);
            
        }
        
    }
    
    ///显示提示信息
    /// userEnabled = true 用户点击会被遮挡
    func showGoodHint(hint: String?,userEnabled:Bool = false) {
        
        DispatchQueue.main.async { [weak self] in  // 切换到主队列
            guard let self = self else { return }
            
            let view = getWindow
            let red = UIColor.FromHex(rgb: 0xFB5656)
            
            let hud = MBProgressHUD.showAdded(to: view, animated: true);
            
            hud.isUserInteractionEnabled = userEnabled
            hud.mode = .text;
            hud.detailsLabel.textColor = .green
            hud.detailsLabel.text = hint;
            hud.detailsLabel.font = UIFont.systemFont(ofSize: 13)
            hud.margin = 10;
            
            // 去掉半透明背景
            hud.bezelView.style = .solidColor
            // 修改背景色
            hud.bezelView.color = .FromHex(rgb: 0xFCF6F6)
            hud.bezelView.layer.cornerRadius = 8
            hud.bezelView.layer.borderWidth = 1
            hud.bezelView.layer.borderColor = red.cgColor
            
            hud.removeFromSuperViewOnHide = true;
            // 显示到屏幕底部起1/4的位置
            hud.offset = CGPoint(x: 0, y: HUD.baseOffsetY )
            hud.hide(animated: true, afterDelay: 2);
            
        }
        
    }
    
    ///显示错误提示信息
    /// userEnabled = true 用户点击会被遮挡
    public static func showErrorHint(hint: String?,userEnabled: Bool = false){
        HUD.shared.showHint(hint: hint, userEnabled: userEnabled, type: .error)
    }
    ///显示错误提示信息
    /// userEnabled = true 用户点击会被遮挡
    func showErrorHint(hint: String?,userEnabled:Bool = false) {
        
        DispatchQueue.main.async { [weak self] in  // 切换到主队列
            guard let self = self else { return }
            
            let view = getWindow
            let red = UIColor.FromHex(rgb: 0xFB5656)
            
            let hud = MBProgressHUD.showAdded(to: view, animated: true);
            
            hud.isUserInteractionEnabled = userEnabled
            hud.mode = .text;
            hud.detailsLabel.textColor = red
            hud.detailsLabel.text = hint;
            hud.detailsLabel.font = UIFont.systemFont(ofSize: 13)
            hud.margin = 10;
            
            // 去掉半透明背景
            hud.bezelView.style = .solidColor
            // 修改背景色
            hud.bezelView.color = .FromHex(rgb: 0xFCF6F6)
            hud.bezelView.layer.cornerRadius = 8
            hud.bezelView.layer.borderWidth = 1
            hud.bezelView.layer.borderColor = red.cgColor
            
            hud.removeFromSuperViewOnHide = true;
            hud.offset = CGPoint(x: 0, y: HUD.baseOffsetY )
            hud.hide(animated: true, afterDelay: 2);
            
        }
        
    }
    
}
